/*
 * Eugen Stoian <stoian.e@gmail.com> 2010
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "ParametricObject3D.h"
#include "Parameter.h"
#include "Expression3D.h"
#include "Project.h"

ParametricObject3D::ParametricObject3D(QObject * parent)
:PlotableObject3D(parent),_expression(0){
}

ParametricObject3D::ParametricObject3D(const QString & expressionName, QObject * parent)
:PlotableObject3D(parent),_expression(0){
    setExpression(expressionName);
}

ParametricObject3D::ParametricObject3D(const Expression3D * exp, QObject * parent)
:PlotableObject3D(parent),_expression(0){
    setExpression(exp);
}

ParametricObject3D::~ParametricObject3D(){
}

void ParametricObject3D::setExpression(const QString & expressionName){
    Project *project = qobject_cast<Project*>(parent());
    if(project){
        setExpression(project->findExpression(expressionName));
    }
}

void ParametricObject3D::setExpression(const Expression3D * exp){
    if(_expression)
        disconnect(_expression,SIGNAL(destroyed()),this,SLOT(onExpressionDestroyed()));
    cleanUp();
    _expression = exp;
    if(_expression){
        connect(_expression,SIGNAL(destroyed()),this,SLOT(onExpressionDestroyed()));
        const QStringList & vars = _expression->variablesList();
        for(int i=0;i<vars.count();++i){
            Parameter* param = new Parameter(vars[i],Interval(0.0,1.0),20,this);
            _parameters.append(param);
        }
    }
    emit changed();
}

const Expression3D * ParametricObject3D::expression() const{
    return _expression;
}

QList< Parameter * > & ParametricObject3D::parameters(){
    return _parameters;
}

QList<const Parameter*> ParametricObject3D::fixedParameters() const{
    QList<const Parameter*> list;
    QList<Parameter*>::const_iterator it;
    for(it = _parameters.begin(); it != _parameters.end(); ++it)
        if((*it)->isFixed())
            list.append(*it);
    return list;
}

QList<Parameter*> ParametricObject3D::fixedParameters(){
    QList<Parameter*> list;
    QList<Parameter*>::iterator it;
    for(it = _parameters.begin(); it != _parameters.end(); ++it)
        if((*it)->isFixed())
            list.append(*it);
    return list;
}

QList<const Parameter*> ParametricObject3D::autoParameters() const{
    QList<const Parameter*> list;
    QList<Parameter*>::const_iterator it;
    for(it = _parameters.begin(); it != _parameters.end(); ++it)
        if((*it)->isAuto())
            list.append(*it);
    return list;
}

QList<Parameter*> ParametricObject3D::autoParameters(){
    QList<Parameter*> list;
    QList<Parameter*>::iterator it;
    for(it = _parameters.begin(); it != _parameters.end(); ++it)
        if((*it)->isAuto())
            list.append(*it);
    return list;
}

const QList< Parameter * > & ParametricObject3D::parameters() const{
    return _parameters;
}

void ParametricObject3D::updateParameter(const Parameter* param){
    if(param){
        QString name = param->objectName();
        QList<Parameter*>::iterator it;
        for(it = _parameters.begin(); it != _parameters.end(); ++it){
            if((*it)->objectName()==name){
                delete *it;
                *it = qobject_cast<Parameter*>(param->clone());
            }
        }
    }
}

bool ParametricObject3D::isValid() const {
    return _expression;
}

bool ParametricObject3D::isEmpty() const {
    return true;
}

void ParametricObject3D::onExpressionDestroyed(){
    _expression = 0;
    cleanUp();
    emit changed();
}

void ParametricObject3D::cleanUp() {
    PlotableObject3D::cleanUp();
    QList<Parameter *>::iterator it;
    for(it = _parameters.begin(); it != _parameters.end(); ++it)
        delete *it;
    _parameters.clear();
}

QDataStream& operator<<(QDataStream& stream, const ParametricObject3D& c){
    stream << c.type();
    stream << c.objectName();
    stream << c.color();
    if(c.expression()){
        stream << true;
        stream << c._expression->objectName();
        stream << (int)c._parameters.count();
        for(int i = 0; i < c._parameters.count();++i)
            stream << *(c._parameters.at(i));
    }
    else
        stream << false;
    return stream;
}

QDataStream& operator>>(QDataStream& stream, ParametricObject3D& c){
    QString name;
    stream >> name;
    c.setObjectName(name);
    QColor color;
    stream >> color;
    c.setColor(color);
    bool valid;
    stream >> valid;
    if(valid){
        stream >> name;
        c.setExpression(name);
        int count;
        stream >> count;
        for(int i =0; i < count; ++i){
            QString paramName;
            stream >> paramName;
            Parameter * p = new Parameter(paramName);
            stream >> *p;
            c.updateParameter(p);
            delete p;
        }
    }
    return stream;
}
